﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;

namespace Sebra.Academico.InfraEstrutura.Core.Helper
{
    public class WebFormHelper
    {
        public static void PreencherLista<T>(IList<T> pLista, ListControl pControle, bool pInsereOpcaoTodos = true)
        {
            if (pControle != null)
            {
                pControle.DataSource = pLista;
                pControle.DataValueField = "ID";
                pControle.DataTextField = "Nome";
                pControle.DataBind();

                if (pInsereOpcaoTodos)
                    pControle.Items.Insert(0, new ListItem("-- Todos --", string.Empty));
            }
        }

        public static void SetarValorNaCombo(string pValor, DropDownList pCombo)
        {
            pCombo.Items.FindByValue(pValor).Selected = true;
        }

        public static void PreencherGrid<T>(IList<T> plista, GridView pGrid)
        {
            if (plista != null && plista.Count > 0)
            {
                pGrid.DataSource = plista;
                pGrid.DataBind();
            }
        }


        public static void ExibirMensagem(string pMensagem)
        {
            string cleanMessage = pMensagem.Replace("\\", "\\\\");

            cleanMessage = pMensagem.Replace("'", "\\'").Replace("\n", "\\n").Replace("\r", "\\r");

            string script = "alert('" + cleanMessage + "');";

            Page page = HttpContext.Current.CurrentHandler as Page;

            if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alerta"))
            {
                page.ClientScript.RegisterClientScriptBlock(page.GetType(), pMensagem, script.ToString());
            }
        }
    }
}
